export default {
    namespaced: true,
    state: () => ({
        // 获取list
        list: JSON.parse(localStorage.getItem("list") || "[]"),
        // 判断全选
        Sel: "all",
        ent: ''
    }),
    mutations: {
        // 获取输入框的值
        item(state, payload) {
            state.ent = payload
        },
        // 把输入框的值添加到数组中
        add(state) {
            this.item
            if (state.ent.newVal == "") return alert("内容不能为空！");
            let id =
                state.list.length > 0 ? state.list[state.list.length - 1].id + 1 : 100;
            state.list.push({
                id,
                name: state.ent.newVal,
                isDone: false,
            });
            state.ent.newVal = "";
        },
        // 删除
        del(state, payload) {
            let index = state.list.findIndex((val) => val.id === payload.id);
            state.list.splice(index, 1);
        },
        //点击切换
        change(state, payload) {
            state.Sel = payload.Sel;
        },
        // 清除已完成
        clear(state) {
            state.list = state.list.filter((item) => !item.isDone);
        },
    },
    getters: {
        //点击切换
        showList(state) {
            if (state.Sel == "no") {
                return state.list.filter((item) => !item.isDone);
            } else if (state.Sel == "yes") {
                return state.list.filter((item) => item.isDone);
            } else {
                return state.list;
            }
        },
    }
}